#!/bin/sh

# run_js_file [--no-unhandled-exception-stack] sk-to-js-file [args...]

# Runs skip code which has been converted to JavaScript with build/bin/skip_to_js.

#
# NODE_PARAMS can be used to set parameters to NODE
# Examples include:
#  debug - Start with command line node debugger
#  --prof - Dump a V8 profile file named isolate-XXXX-v8.log to be processed with 'node --prof-process isolate-XXX-v8.log > prof.out'
#  --inspect - Start with inspector debugger: https://nodejs.org/en/docs/inspector/
#  --inspect --inspect-brk - Start with inspector debugger and break waiting for debugger connection before continuing

DIR=$(dirname "$0")

EXTRA_OLD_SPACE="${EXTRA_OLD_SPACE:+--max_old_space_size=8192}"

if [ -z "${NODE}" ]; then
    if [ -f "${DIR}/../build/CMakeCache.txt" ]; then
        export NODE=$(grep 'NODE:FILEPATH=' "${DIR}/../build/CMakeCache.txt" | sed -e 's/NODE:FILEPATH=\(.*\)/\1/')
    else
        export NODE=node
    fi
fi

JSLIB_RELATIVE="${JSLIB:-$(dirname "$0")/../tests/runtime/js}"
export JSLIB=`echo "$(cd $(dirname ${JSLIB_RELATIVE}); pwd)/$(basename ${JSLIB_RELATIVE})"`
$NODE ${NODE_PARAMS} ${EXTRA_OLD_SPACE} --stack_size=8192 ${JSLIB}/main.js "$@"
